%     VIRTUAL LAMINATE - программа для получения эффективных свойств слоистых пластин
%     Copyright (C) 2020  Михаил Козлов
%     Сайт проекта: <http://www.fastenerdoctor.ru/>
%     Адрес электронной почты для связи с разработчиком:
%     <doctor.fastener@gmail.com>
% 
%     Эта программа является свободным программным обеспечением: Вы можете
%     распространять ее и/или изменять, соблюдая условия Универсальной общественной
%     лицензии GNU, опубликованной Фондом свободного программного обеспечения,
%     либо редакции 3, либо (на Ваше усмотрение) любой редакции, выпущенной позже.
% 
%     Эта программа распространяется в надежде на то, что она окажется полезной,
%     но БЕЗ КАКИХ-ЛИБО ГАРАНТИЙ, включая даже подразумеваемую гарантию ТОВАРНОЙ ПРИГОДНОСТИ,
%     либо ПРИГОДНОСТИ ДЛЯ ОПРЕДЕЛЕННЫХ ЦЕЛЕЙ. Ознакомьтесь с Универсальной общественной
%     лицензией GNU для получения более подробной информации.
% 
%     Вы должны были получить копию Универсальной общественной лицензии GNU
%     вместе с этой программой. Если Вы ее не получили, то перейдите по адресу:
%     <http://www.gnu.org/licenses/>.
% 
%     This program is free software: you can redistribute it and/or modify
%     it under the terms of the GNU General Public License as published by
%     the Free Software Foundation, either version 3 of the License, or
%     (at your option) any later version.
% 
%     This program is distributed in the hope that it will be useful,
%     but WITHOUT ANY WARRANTY; without even the implied warranty of
%     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
%     GNU General Public License for more details.
% 
%     You should have received a copy of the GNU General Public License
%     along with this program.  If not, see <https://www.gnu.org/licenses/>.

function Write_Input_Mechanical(Job_Name, Work_Directory, NODES, ELEMENTS, Cell_Mesh_Type, RVE_Coordinates, Node_Merge_Tolerance, CTE_Mode, Mechanical_BC_TYPE, Constraint_Directions, Ply_Elements, Materials, Stacking, Element_Order)

global ErrCode;

% Формат числовых данных в файле
node_coord_format = '%.12e';
orientation_format = '%.12e';
mat_prop_format = '%.12e';
equation_format = '%.12e';
bc_format = '%.12e';

% Имена детали и модели
Part_Name = 'LAMINATE_RVE';
Model_Name = Job_Name;

% Габариты ячейки периодичности
LX = RVE_Coordinates(1,2) - RVE_Coordinates(1,1);
LY = RVE_Coordinates(2,2) - RVE_Coordinates(2,1);
LZ = RVE_Coordinates(3,2) - RVE_Coordinates(3,1);

% Проверка нумерации узлов
for i=1:size(NODES,1)
    if (NODES(i,1) ~= i)
        fprintf (1, '\nОшибка функции Write_Input_Mechanical: некорректная нумерация узлов!\n');
        ErrCode = -1;
        return;
    end
end

% Проверка нумерации элементов
for i=1:size(ELEMENTS,1)
    if (ELEMENTS(i,1) ~= i)
        fprintf (1, '\nОшибка функции Write_Input_Mechanical: некорректная нумерация элементов!\n');
        ErrCode = -1;
        return;
    end
end

% Путь к файлу
File_name = Job_Name;
File_path = [Work_Directory '\' File_name '.inp'];

% Открытие файла
Fout = fopen(File_path, 'wt');

if (Fout < 0)
    fprintf (1, '\nОшибка функции Write_Input_Mechanical: не удалось открыть для записи файл [%s] !\n', File_path);
    ErrCode = -1;
    return;
end

fprintf (Fout, '*Heading\n');

fprintf (Fout, '** Job name: %s Model name: %s\n', Job_Name, Model_Name);
fprintf (Fout, '** Generated by: Virtual_Laminate\n');

fprintf (Fout, '*Preprint, echo=NO, model=NO, history=NO, contact=NO\n');

fprintf (Fout, '**\n');
fprintf (Fout, '** PARTS\n');
fprintf (Fout, '**\n');

fprintf (Fout, '*Part, name=%s\n', Part_Name);

fprintf (Fout, '*Node\n');

for i=1:size(NODES, 1)
    fprintf (Fout, ['%i, ' node_coord_format ', ' node_coord_format ', ' node_coord_format '\n'], NODES(i,1), NODES(i,2), NODES(i,3), NODES(i,4));
end

switch Element_Order
    
    case 'LINEAR'
        
        fprintf (Fout, '*Element, type=C3D8I\n');

        for i=1:size(ELEMENTS,1)
            fprintf (Fout, '%i, %i, %i, %i, %i, %i, %i, %i, %i\n', ELEMENTS(i,1), ELEMENTS(i,5), ELEMENTS(i,6), ELEMENTS(i,7), ...
                                                                   ELEMENTS(i,8), ELEMENTS(i,9), ELEMENTS(i,10), ELEMENTS(i,11), ...
                                                                   ELEMENTS(i,12));
        end
        
    case 'QUADRATIC'
        
        fprintf (Fout, '*Element, type=C3D20R\n');

        for i=1:size(ELEMENTS,1)
            fprintf (Fout, '%i, %i, %i, %i, %i, %i, %i, %i, %i, %i, %i, %i, %i, %i, %i, %i,\n', ELEMENTS(i,1), ELEMENTS(i,5), ELEMENTS(i,6), ELEMENTS(i,7), ...
                                                                                               ELEMENTS(i,8), ELEMENTS(i,9), ELEMENTS(i,10), ELEMENTS(i,11), ...
                                                                                               ELEMENTS(i,12), ELEMENTS(i,13), ELEMENTS(i,14), ELEMENTS(i,15), ...
                                                                                               ELEMENTS(i,16), ELEMENTS(i,17), ELEMENTS(i,18), ELEMENTS(i,19));
            fprintf (Fout, '   %i, %i, %i, %i, %i\n', ELEMENTS(i,20), ELEMENTS(i,21), ELEMENTS(i,22), ELEMENTS(i,23), ELEMENTS(i,24));
        end
    
end

fprintf (Fout, '*Nset, nset=ALL_NODES, generate\n');
fprintf (Fout, ' %i, %i, 1\n', 1, NODES(size(NODES,1),1));

fprintf (Fout, '*Elset, elset=ALL_ELEMENTS, generate\n');
fprintf (Fout, ' %i, %i, 1\n', 1, ELEMENTS(size(ELEMENTS,1),1));

N_digits = numel(num2str(Stacking(size(Stacking,1),1)));

format_line = ['*Elset, elset=Ply_%.' sprintf('%i', N_digits) 'i\n'];

N_previous_items = 0;

for i=1:size(Stacking,1)
        
    fprintf (Fout, format_line, Stacking(i,1));
    
    Write_Set(Fout, ELEMENTS(N_previous_items + 1 : N_previous_items + Ply_Elements(i), 1));
    
    N_previous_items = N_previous_items + Ply_Elements(i);
    
end

format_line = ['*Orientation, name=Ori_Coord_%.' sprintf('%i', N_digits) 'i\n'];

format_line_2 = [orientation_format ', ' orientation_format ', ' orientation_format ', ' orientation_format ', ' orientation_format ', ' orientation_format ', ' orientation_format ', ' orientation_format ', ' orientation_format '\n'];

Origin_High = 0;

for i=1:size(Stacking,1)
    
    fprintf (Fout, format_line, Stacking(i,1));
    
    fprintf (Fout, format_line_2,  cosd(Stacking(i,2)), sind(Stacking(i,2)), Origin_High + Materials{Stacking(i,3)}.t/2, ...
                                  -sind(Stacking(i,2)), cosd(Stacking(i,2)), Origin_High + Materials{Stacking(i,3)}.t/2, ...
                                                                       0, 0, Origin_High + Materials{Stacking(i,3)}.t/2);
    fprintf (Fout, '3, 0.\n');
    
    Origin_High = Origin_High + Materials{Stacking(i,3)}.t;
    
end

format_line = ['** Section: Section_Ply_%.' sprintf('%i', N_digits) 'i\n'];

format_line_2 = ['*Solid Section, elset=Ply_%.' sprintf('%i', N_digits) 'i, orientation=Ori_Coord_%.' sprintf('%i', N_digits) 'i, material=Material_%i\n'];

for i=1:size(Stacking,1)
    
    fprintf (Fout, format_line, Stacking(i,1));
    
    fprintf (Fout, format_line_2, Stacking(i,1), Stacking(i,1), Stacking(i,3));
    
    fprintf (Fout, ',\n');
    
end

fprintf (Fout, '*End Part\n');

fprintf (Fout, '**\n');
fprintf (Fout, '** ASSEMBLY\n');
fprintf (Fout, '**\n');

fprintf (Fout, '*Assembly, name=Assembly\n');
fprintf (Fout, '**\n');
fprintf (Fout, '*Instance, name=%s-1, part=%s\n', Part_Name, Part_Name);
fprintf (Fout, '*End Instance\n');
fprintf (Fout, '**\n');

fprintf (Fout, '*** ConstraintsDriver0 = e_x\n');
fprintf (Fout, '*** ConstraintsDriver1 = e_y\n');
fprintf (Fout, '*** ConstraintsDriver2 = e_z\n');
fprintf (Fout, '*** ConstraintsDriver3 = e_xy\n');
fprintf (Fout, '*** ConstraintsDriver4 = e_xz\n');
fprintf (Fout, '*** ConstraintsDriver5 = e_yz\n');

fprintf (Fout, '*Node\n');

for i=0:5
    fprintf (Fout, '%i, 0., 0., 0.\n', i+1);
end

for i=0:5
    fprintf (Fout, '*NSet, NSet=ConstraintsDriver%i\n', i);
    fprintf (Fout, '%i\n', i+1);
end

Nset_FaceA = Generate_Nset(NODES, RVE_Coordinates(1,2)-Node_Merge_Tolerance, RVE_Coordinates(1,2)+Node_Merge_Tolerance, RVE_Coordinates(2,1)+Node_Merge_Tolerance, RVE_Coordinates(2,2)-Node_Merge_Tolerance, RVE_Coordinates(3,1)+Node_Merge_Tolerance, RVE_Coordinates(3,2)-Node_Merge_Tolerance);
Nset_FaceB = Generate_Nset(NODES, RVE_Coordinates(1,1)-Node_Merge_Tolerance, RVE_Coordinates(1,1)+Node_Merge_Tolerance, RVE_Coordinates(2,1)+Node_Merge_Tolerance, RVE_Coordinates(2,2)-Node_Merge_Tolerance, RVE_Coordinates(3,1)+Node_Merge_Tolerance, RVE_Coordinates(3,2)-Node_Merge_Tolerance);
Nset_FaceC = Generate_Nset(NODES, RVE_Coordinates(1,1)+Node_Merge_Tolerance, RVE_Coordinates(1,2)-Node_Merge_Tolerance, RVE_Coordinates(2,2)-Node_Merge_Tolerance, RVE_Coordinates(2,2)+Node_Merge_Tolerance, RVE_Coordinates(3,1)+Node_Merge_Tolerance, RVE_Coordinates(3,2)-Node_Merge_Tolerance);
Nset_FaceD = Generate_Nset(NODES, RVE_Coordinates(1,1)+Node_Merge_Tolerance, RVE_Coordinates(1,2)-Node_Merge_Tolerance, RVE_Coordinates(2,1)-Node_Merge_Tolerance, RVE_Coordinates(2,1)+Node_Merge_Tolerance, RVE_Coordinates(3,1)+Node_Merge_Tolerance, RVE_Coordinates(3,2)-Node_Merge_Tolerance);
Nset_FaceE = Generate_Nset(NODES, RVE_Coordinates(1,1)+Node_Merge_Tolerance, RVE_Coordinates(1,2)-Node_Merge_Tolerance, RVE_Coordinates(2,1)+Node_Merge_Tolerance, RVE_Coordinates(2,2)-Node_Merge_Tolerance, RVE_Coordinates(3,2)-Node_Merge_Tolerance, RVE_Coordinates(3,2)+Node_Merge_Tolerance);
Nset_FaceF = Generate_Nset(NODES, RVE_Coordinates(1,1)+Node_Merge_Tolerance, RVE_Coordinates(1,2)-Node_Merge_Tolerance, RVE_Coordinates(2,1)+Node_Merge_Tolerance, RVE_Coordinates(2,2)-Node_Merge_Tolerance, RVE_Coordinates(3,1)-Node_Merge_Tolerance, RVE_Coordinates(3,1)+Node_Merge_Tolerance);

if (isempty(Nset_FaceA))
    fprintf (1, '\nОшибка: не удалось найти узлы для сэта FaceA !\n');
    fprintf (1, 'Измените параметры построения сетки.\n');
    fclose(Fout);
    ErrCode = -1;
    return;
end

if (isempty(Nset_FaceB))
    fprintf (1, '\nОшибка: не удалось найти узлы для сэта FaceB !\n');
    fprintf (1, 'Измените параметры построения сетки.\n');
    fclose(Fout);
    ErrCode = -1;
    return;
end

if (isempty(Nset_FaceC))
    fprintf (1, '\nОшибка: не удалось найти узлы для сэта FaceC !\n');
    fprintf (1, 'Измените параметры построения сетки.\n');
    fclose(Fout);
    ErrCode = -1;
    return;
end

if (isempty(Nset_FaceD))
    fprintf (1, '\nОшибка: не удалось найти узлы для сэта FaceD !\n');
    fprintf (1, 'Измените параметры построения сетки.\n');
    fclose(Fout);
    ErrCode = -1;
    return;
end

if (isempty(Nset_FaceE))
    fprintf (1, '\nОшибка: не удалось найти узлы для сэта FaceE !\n');
    fprintf (1, 'Измените параметры построения сетки.\n');
    fclose(Fout);
    ErrCode = -1;
    return;
end

if (isempty(Nset_FaceF))
    fprintf (1, '\nОшибка: не удалось найти узлы для сэта FaceF !\n');
    fprintf (1, 'Измените параметры построения сетки.\n');
    fclose(Fout);
    ErrCode = -1;
    return;
end

if (numel(Nset_FaceA) ~= numel(Nset_FaceB))
    fprintf (1, '\nОшибка: размер сэта FaceA не совпадает с размером сэта FaceB !\n');
    fclose(Fout);
    ErrCode = -1;
    return;
end

if (numel(Nset_FaceC) ~= numel(Nset_FaceD))
    fprintf (1, '\nОшибка: размер сэта FaceC не совпадает с размером сэта FaceD !\n');
    fclose(Fout);
    ErrCode = -1;
    return;
end

if (numel(Nset_FaceE) ~= numel(Nset_FaceF))
    fprintf (1, '\nОшибка: размер сэта FaceE не совпадает с размером сэта FaceF !\n');
    fclose(Fout);
    ErrCode = -1;
    return;
end

Nset_FaceB = Match_Nodes(Nset_FaceB, Nset_FaceA, NODES, 1, Node_Merge_Tolerance);

if (ErrCode < 0)
    fclose(Fout);
    return;
end

Nset_FaceD = Match_Nodes(Nset_FaceD, Nset_FaceC, NODES, 2, Node_Merge_Tolerance);

if (ErrCode < 0)
    fclose(Fout);
    return;
end

Nset_FaceF = Match_Nodes(Nset_FaceF, Nset_FaceE, NODES, 3, Node_Merge_Tolerance);

if (ErrCode < 0)
    fclose(Fout);
    return;
end

fprintf (Fout, '*NSet, NSet=FaceA, instance=%s-1, Unsorted\n', Part_Name);
Write_Set(Fout, Nset_FaceA);

fprintf (Fout, '*NSet, NSet=FaceB, instance=%s-1, Unsorted\n', Part_Name);
Write_Set(Fout, Nset_FaceB);

fprintf (Fout, '*NSet, NSet=FaceC, instance=%s-1, Unsorted\n', Part_Name);
Write_Set(Fout, Nset_FaceC);

fprintf (Fout, '*NSet, NSet=FaceD, instance=%s-1, Unsorted\n', Part_Name);
Write_Set(Fout, Nset_FaceD);

fprintf (Fout, '*NSet, NSet=FaceE, instance=%s-1, Unsorted\n', Part_Name);
Write_Set(Fout, Nset_FaceE);

fprintf (Fout, '*NSet, NSet=FaceF, instance=%s-1, Unsorted\n', Part_Name);
Write_Set(Fout, Nset_FaceF);

Nset_Edge1 = Generate_Nset(NODES, RVE_Coordinates(1,1)-Node_Merge_Tolerance, RVE_Coordinates(1,1)+Node_Merge_Tolerance, RVE_Coordinates(2,1)-Node_Merge_Tolerance, RVE_Coordinates(2,1)+Node_Merge_Tolerance, RVE_Coordinates(3,1)+Node_Merge_Tolerance, RVE_Coordinates(3,2)-Node_Merge_Tolerance);
Nset_Edge2 = Generate_Nset(NODES, RVE_Coordinates(1,2)-Node_Merge_Tolerance, RVE_Coordinates(1,2)+Node_Merge_Tolerance, RVE_Coordinates(2,1)-Node_Merge_Tolerance, RVE_Coordinates(2,1)+Node_Merge_Tolerance, RVE_Coordinates(3,1)+Node_Merge_Tolerance, RVE_Coordinates(3,2)-Node_Merge_Tolerance);
Nset_Edge3 = Generate_Nset(NODES, RVE_Coordinates(1,2)-Node_Merge_Tolerance, RVE_Coordinates(1,2)+Node_Merge_Tolerance, RVE_Coordinates(2,2)-Node_Merge_Tolerance, RVE_Coordinates(2,2)+Node_Merge_Tolerance, RVE_Coordinates(3,1)+Node_Merge_Tolerance, RVE_Coordinates(3,2)-Node_Merge_Tolerance);
Nset_Edge4 = Generate_Nset(NODES, RVE_Coordinates(1,1)-Node_Merge_Tolerance, RVE_Coordinates(1,1)+Node_Merge_Tolerance, RVE_Coordinates(2,2)-Node_Merge_Tolerance, RVE_Coordinates(2,2)+Node_Merge_Tolerance, RVE_Coordinates(3,1)+Node_Merge_Tolerance, RVE_Coordinates(3,2)-Node_Merge_Tolerance);
Nset_Edge5 = Generate_Nset(NODES, RVE_Coordinates(1,1)-Node_Merge_Tolerance, RVE_Coordinates(1,1)+Node_Merge_Tolerance, RVE_Coordinates(2,1)+Node_Merge_Tolerance, RVE_Coordinates(2,2)-Node_Merge_Tolerance, RVE_Coordinates(3,1)-Node_Merge_Tolerance, RVE_Coordinates(3,1)+Node_Merge_Tolerance);
Nset_Edge6 = Generate_Nset(NODES, RVE_Coordinates(1,2)-Node_Merge_Tolerance, RVE_Coordinates(1,2)+Node_Merge_Tolerance, RVE_Coordinates(2,1)+Node_Merge_Tolerance, RVE_Coordinates(2,2)-Node_Merge_Tolerance, RVE_Coordinates(3,1)-Node_Merge_Tolerance, RVE_Coordinates(3,1)+Node_Merge_Tolerance);
Nset_Edge7 = Generate_Nset(NODES, RVE_Coordinates(1,2)-Node_Merge_Tolerance, RVE_Coordinates(1,2)+Node_Merge_Tolerance, RVE_Coordinates(2,1)+Node_Merge_Tolerance, RVE_Coordinates(2,2)-Node_Merge_Tolerance, RVE_Coordinates(3,2)-Node_Merge_Tolerance, RVE_Coordinates(3,2)+Node_Merge_Tolerance);
Nset_Edge8 = Generate_Nset(NODES, RVE_Coordinates(1,1)-Node_Merge_Tolerance, RVE_Coordinates(1,1)+Node_Merge_Tolerance, RVE_Coordinates(2,1)+Node_Merge_Tolerance, RVE_Coordinates(2,2)-Node_Merge_Tolerance, RVE_Coordinates(3,2)-Node_Merge_Tolerance, RVE_Coordinates(3,2)+Node_Merge_Tolerance);
Nset_Edge9 = Generate_Nset(NODES, RVE_Coordinates(1,1)+Node_Merge_Tolerance, RVE_Coordinates(1,2)-Node_Merge_Tolerance, RVE_Coordinates(2,1)-Node_Merge_Tolerance, RVE_Coordinates(2,1)+Node_Merge_Tolerance, RVE_Coordinates(3,1)-Node_Merge_Tolerance, RVE_Coordinates(3,1)+Node_Merge_Tolerance);
Nset_Edge10 = Generate_Nset(NODES, RVE_Coordinates(1,1)+Node_Merge_Tolerance, RVE_Coordinates(1,2)-Node_Merge_Tolerance, RVE_Coordinates(2,2)-Node_Merge_Tolerance, RVE_Coordinates(2,2)+Node_Merge_Tolerance, RVE_Coordinates(3,1)-Node_Merge_Tolerance, RVE_Coordinates(3,1)+Node_Merge_Tolerance);
Nset_Edge11 = Generate_Nset(NODES, RVE_Coordinates(1,1)+Node_Merge_Tolerance, RVE_Coordinates(1,2)-Node_Merge_Tolerance, RVE_Coordinates(2,2)-Node_Merge_Tolerance, RVE_Coordinates(2,2)+Node_Merge_Tolerance, RVE_Coordinates(3,2)-Node_Merge_Tolerance, RVE_Coordinates(3,2)+Node_Merge_Tolerance);
Nset_Edge12 = Generate_Nset(NODES, RVE_Coordinates(1,1)+Node_Merge_Tolerance, RVE_Coordinates(1,2)-Node_Merge_Tolerance, RVE_Coordinates(2,1)-Node_Merge_Tolerance, RVE_Coordinates(2,1)+Node_Merge_Tolerance, RVE_Coordinates(3,2)-Node_Merge_Tolerance, RVE_Coordinates(3,2)+Node_Merge_Tolerance);

if (isempty(Nset_Edge1))
    fprintf (1, '\nОшибка: не удалось найти узлы для сэта Edge1 !\n');
    fprintf (1, 'Измените параметры построения сетки.\n');
    fclose(Fout);
    ErrCode = -1;
    return;
end

if (isempty(Nset_Edge2))
    fprintf (1, '\nОшибка: не удалось найти узлы для сэта Edge2 !\n');
    fprintf (1, 'Измените параметры построения сетки.\n');
    fclose(Fout);
    ErrCode = -1;
    return;
end

if (isempty(Nset_Edge3))
    fprintf (1, '\nОшибка: не удалось найти узлы для сэта Edge3 !\n');
    fprintf (1, 'Измените параметры построения сетки.\n');
    fclose(Fout);
    ErrCode = -1;
    return;
end

if (isempty(Nset_Edge4))
    fprintf (1, '\nОшибка: не удалось найти узлы для сэта Edge4 !\n');
    fprintf (1, 'Измените параметры построения сетки.\n');
    fclose(Fout);
    ErrCode = -1;
    return;
end

if (isempty(Nset_Edge5))
    fprintf (1, '\nОшибка: не удалось найти узлы для сэта Edge5 !\n');
    fprintf (1, 'Измените параметры построения сетки.\n');
    fclose(Fout);
    ErrCode = -1;
    return;
end

if (isempty(Nset_Edge6))
    fprintf (1, '\nОшибка: не удалось найти узлы для сэта Edge6 !\n');
    fprintf (1, 'Измените параметры построения сетки.\n');
    fclose(Fout);
    ErrCode = -1;
    return;
end

if (isempty(Nset_Edge7))
    fprintf (1, '\nОшибка: не удалось найти узлы для сэта Edge7 !\n');
    fprintf (1, 'Измените параметры построения сетки.\n');
    fclose(Fout);
    ErrCode = -1;
    return;
end

if (isempty(Nset_Edge8))
    fprintf (1, '\nОшибка: не удалось найти узлы для сэта Edge8 !\n');
    fprintf (1, 'Измените параметры построения сетки.\n');
    fclose(Fout);
    ErrCode = -1;
    return;
end

if (isempty(Nset_Edge9))
    fprintf (1, '\nОшибка: не удалось найти узлы для сэта Edge9 !\n');
    fprintf (1, 'Измените параметры построения сетки.\n');
    fclose(Fout);
    ErrCode = -1;
    return;
end

if (isempty(Nset_Edge10))
    fprintf (1, '\nОшибка: не удалось найти узлы для сэта Edge10 !\n');
    fprintf (1, 'Измените параметры построения сетки.\n');
    fclose(Fout);
    ErrCode = -1;
    return;
end

if (isempty(Nset_Edge11))
    fprintf (1, '\nОшибка: не удалось найти узлы для сэта Edge11 !\n');
    fprintf (1, 'Измените параметры построения сетки.\n');
    fclose(Fout);
    ErrCode = -1;
    return;
end

if (isempty(Nset_Edge12))
    fprintf (1, '\nОшибка: не удалось найти узлы для сэта Edge12 !\n');
    fprintf (1, 'Измените параметры построения сетки.\n');
    fclose(Fout);
    ErrCode = -1;
    return;
end

if (numel(Nset_Edge2) ~= numel(Nset_Edge1))
    fprintf (1, '\nОшибка: размер сэта Edge2 не совпадает с размером сэта Edge1 !\n');
    fclose(Fout);
    ErrCode = -1;
    return;
end

if (numel(Nset_Edge3) ~= numel(Nset_Edge1))
    fprintf (1, '\nОшибка: размер сэта Edge3 не совпадает с размером сэта Edge1 !\n');
    fclose(Fout);
    ErrCode = -1;
    return;
end

if (numel(Nset_Edge4) ~= numel(Nset_Edge1))
    fprintf (1, '\nОшибка: размер сэта Edge4 не совпадает с размером сэта Edge1 !\n');
    fclose(Fout);
    ErrCode = -1;
    return;
end

if (numel(Nset_Edge6) ~= numel(Nset_Edge5))
    fprintf (1, '\nОшибка: размер сэта Edge6 не совпадает с размером сэта Edge5 !\n');
    fclose(Fout);
    ErrCode = -1;
    return;
end

if (numel(Nset_Edge7) ~= numel(Nset_Edge5))
    fprintf (1, '\nОшибка: размер сэта Edge7 не совпадает с размером сэта Edge5 !\n');
    fclose(Fout);
    ErrCode = -1;
    return;
end

if (numel(Nset_Edge8) ~= numel(Nset_Edge5))
    fprintf (1, '\nОшибка: размер сэта Edge8 не совпадает с размером сэта Edge5 !\n');
    fclose(Fout);
    ErrCode = -1;
    return;
end

if (numel(Nset_Edge10) ~= numel(Nset_Edge9))
    fprintf (1, '\nОшибка: размер сэта Edge10 не совпадает с размером сэта Edge9 !\n');
    fclose(Fout);
    ErrCode = -1;
    return;
end

if (numel(Nset_Edge11) ~= numel(Nset_Edge9))
    fprintf (1, '\nОшибка: размер сэта Edge11 не совпадает с размером сэта Edge9 !\n');
    fclose(Fout);
    ErrCode = -1;
    return;
end

if (numel(Nset_Edge12) ~= numel(Nset_Edge9))
    fprintf (1, '\nОшибка: размер сэта Edge12 не совпадает с размером сэта Edge9 !\n');
    fclose(Fout);
    ErrCode = -1;
    return;
end

Nset_Edge1 = Sort_Nodes(Nset_Edge1, NODES, 3);
Nset_Edge2 = Sort_Nodes(Nset_Edge2, NODES, 3);
Nset_Edge3 = Sort_Nodes(Nset_Edge3, NODES, 3);
Nset_Edge4 = Sort_Nodes(Nset_Edge4, NODES, 3);

Nset_Edge5 = Sort_Nodes(Nset_Edge5, NODES, 2);
Nset_Edge6 = Sort_Nodes(Nset_Edge6, NODES, 2);
Nset_Edge7 = Sort_Nodes(Nset_Edge7, NODES, 2);
Nset_Edge8 = Sort_Nodes(Nset_Edge8, NODES, 2);

Nset_Edge9 = Sort_Nodes(Nset_Edge9, NODES, 1);
Nset_Edge10 = Sort_Nodes(Nset_Edge10, NODES, 1);
Nset_Edge11 = Sort_Nodes(Nset_Edge11, NODES, 1);
Nset_Edge12 = Sort_Nodes(Nset_Edge12, NODES, 1);

fprintf (Fout, '*NSet, NSet=Edge1, instance=%s-1, Unsorted\n', Part_Name);
Write_Set(Fout, Nset_Edge1);

fprintf (Fout, '*NSet, NSet=Edge2, instance=%s-1, Unsorted\n', Part_Name);
Write_Set(Fout, Nset_Edge2);

fprintf (Fout, '*NSet, NSet=Edge3, instance=%s-1, Unsorted\n', Part_Name);
Write_Set(Fout, Nset_Edge3);

fprintf (Fout, '*NSet, NSet=Edge4, instance=%s-1, Unsorted\n', Part_Name);
Write_Set(Fout, Nset_Edge4);

fprintf (Fout, '*NSet, NSet=Edge5, instance=%s-1, Unsorted\n', Part_Name);
Write_Set(Fout, Nset_Edge5);

fprintf (Fout, '*NSet, NSet=Edge6, instance=%s-1, Unsorted\n', Part_Name);
Write_Set(Fout, Nset_Edge6);

fprintf (Fout, '*NSet, NSet=Edge7, instance=%s-1, Unsorted\n', Part_Name);
Write_Set(Fout, Nset_Edge7);

fprintf (Fout, '*NSet, NSet=Edge8, instance=%s-1, Unsorted\n', Part_Name);
Write_Set(Fout, Nset_Edge8);

fprintf (Fout, '*NSet, NSet=Edge9, instance=%s-1, Unsorted\n', Part_Name);
Write_Set(Fout, Nset_Edge9);

fprintf (Fout, '*NSet, NSet=Edge10, instance=%s-1, Unsorted\n', Part_Name);
Write_Set(Fout, Nset_Edge10);

fprintf (Fout, '*NSet, NSet=Edge11, instance=%s-1, Unsorted\n', Part_Name);
Write_Set(Fout, Nset_Edge11);

fprintf (Fout, '*NSet, NSet=Edge12, instance=%s-1, Unsorted\n', Part_Name);
Write_Set(Fout, Nset_Edge12);

Nset_MasterNode1 = Generate_Nset(NODES, RVE_Coordinates(1,1)-Node_Merge_Tolerance, RVE_Coordinates(1,1)+Node_Merge_Tolerance, RVE_Coordinates(2,1)-Node_Merge_Tolerance, RVE_Coordinates(2,1)+Node_Merge_Tolerance, RVE_Coordinates(3,1)-Node_Merge_Tolerance, RVE_Coordinates(3,1)+Node_Merge_Tolerance);
Nset_MasterNode2 = Generate_Nset(NODES, RVE_Coordinates(1,2)-Node_Merge_Tolerance, RVE_Coordinates(1,2)+Node_Merge_Tolerance, RVE_Coordinates(2,1)-Node_Merge_Tolerance, RVE_Coordinates(2,1)+Node_Merge_Tolerance, RVE_Coordinates(3,1)-Node_Merge_Tolerance, RVE_Coordinates(3,1)+Node_Merge_Tolerance);
Nset_MasterNode3 = Generate_Nset(NODES, RVE_Coordinates(1,2)-Node_Merge_Tolerance, RVE_Coordinates(1,2)+Node_Merge_Tolerance, RVE_Coordinates(2,2)-Node_Merge_Tolerance, RVE_Coordinates(2,2)+Node_Merge_Tolerance, RVE_Coordinates(3,1)-Node_Merge_Tolerance, RVE_Coordinates(3,1)+Node_Merge_Tolerance);
Nset_MasterNode4 = Generate_Nset(NODES, RVE_Coordinates(1,1)-Node_Merge_Tolerance, RVE_Coordinates(1,1)+Node_Merge_Tolerance, RVE_Coordinates(2,2)-Node_Merge_Tolerance, RVE_Coordinates(2,2)+Node_Merge_Tolerance, RVE_Coordinates(3,1)-Node_Merge_Tolerance, RVE_Coordinates(3,1)+Node_Merge_Tolerance);
Nset_MasterNode5 = Generate_Nset(NODES, RVE_Coordinates(1,1)-Node_Merge_Tolerance, RVE_Coordinates(1,1)+Node_Merge_Tolerance, RVE_Coordinates(2,1)-Node_Merge_Tolerance, RVE_Coordinates(2,1)+Node_Merge_Tolerance, RVE_Coordinates(3,2)-Node_Merge_Tolerance, RVE_Coordinates(3,2)+Node_Merge_Tolerance);
Nset_MasterNode6 = Generate_Nset(NODES, RVE_Coordinates(1,2)-Node_Merge_Tolerance, RVE_Coordinates(1,2)+Node_Merge_Tolerance, RVE_Coordinates(2,1)-Node_Merge_Tolerance, RVE_Coordinates(2,1)+Node_Merge_Tolerance, RVE_Coordinates(3,2)-Node_Merge_Tolerance, RVE_Coordinates(3,2)+Node_Merge_Tolerance);
Nset_MasterNode7 = Generate_Nset(NODES, RVE_Coordinates(1,2)-Node_Merge_Tolerance, RVE_Coordinates(1,2)+Node_Merge_Tolerance, RVE_Coordinates(2,2)-Node_Merge_Tolerance, RVE_Coordinates(2,2)+Node_Merge_Tolerance, RVE_Coordinates(3,2)-Node_Merge_Tolerance, RVE_Coordinates(3,2)+Node_Merge_Tolerance);
Nset_MasterNode8 = Generate_Nset(NODES, RVE_Coordinates(1,1)-Node_Merge_Tolerance, RVE_Coordinates(1,1)+Node_Merge_Tolerance, RVE_Coordinates(2,2)-Node_Merge_Tolerance, RVE_Coordinates(2,2)+Node_Merge_Tolerance, RVE_Coordinates(3,2)-Node_Merge_Tolerance, RVE_Coordinates(3,2)+Node_Merge_Tolerance);

if (isempty(Nset_MasterNode1))
    fprintf (1, '\nОшибка: не удалось найти узлы для сэта Nset_MasterNode1 !\n');
    fprintf (1, 'Измените параметры построения сетки.\n');
    fclose(Fout);
    ErrCode = -1;
    return;
end

if (isempty(Nset_MasterNode2))
    fprintf (1, '\nОшибка: не удалось найти узлы для сэта Nset_MasterNode2 !\n');
    fprintf (1, 'Измените параметры построения сетки.\n');
    fclose(Fout);
    ErrCode = -1;
    return;
end

if (isempty(Nset_MasterNode3))
    fprintf (1, '\nОшибка: не удалось найти узлы для сэта Nset_MasterNode3 !\n');
    fprintf (1, 'Измените параметры построения сетки.\n');
    fclose(Fout);
    ErrCode = -1;
    return;
end

if (isempty(Nset_MasterNode4))
    fprintf (1, '\nОшибка: не удалось найти узлы для сэта Nset_MasterNode4 !\n');
    fprintf (1, 'Измените параметры построения сетки.\n');
    fclose(Fout);
    ErrCode = -1;
    return;
end

if (isempty(Nset_MasterNode5))
    fprintf (1, '\nОшибка: не удалось найти узлы для сэта Nset_MasterNode5 !\n');
    fprintf (1, 'Измените параметры построения сетки.\n');
    fclose(Fout);
    ErrCode = -1;
    return;
end

if (isempty(Nset_MasterNode6))
    fprintf (1, '\nОшибка: не удалось найти узлы для сэта Nset_MasterNode6 !\n');
    fprintf (1, 'Измените параметры построения сетки.\n');
    fclose(Fout);
    ErrCode = -1;
    return;
end

if (isempty(Nset_MasterNode7))
    fprintf (1, '\nОшибка: не удалось найти узлы для сэта Nset_MasterNode7 !\n');
    fprintf (1, 'Измените параметры построения сетки.\n');
    fclose(Fout);
    ErrCode = -1;
    return;
end

if (isempty(Nset_MasterNode8))
    fprintf (1, '\nОшибка: не удалось найти узлы для сэта Nset_MasterNode8 !\n');
    fprintf (1, 'Измените параметры построения сетки.\n');
    fclose(Fout);
    ErrCode = -1;
    return;
end

if (numel(Nset_MasterNode1) ~= 1)
    fprintf (1, '\nОшибка: недопустимое число узлов в сэте Nset_MasterNode1 !\n');
    fprintf (1, 'Измените параметр Merge_Tolerance_Multiplier.\n');
    fclose(Fout);
    ErrCode = -1;
    return;
end

if (numel(Nset_MasterNode2) ~= 1)
    fprintf (1, '\nОшибка: недопустимое число узлов в сэте Nset_MasterNode2 !\n');
    fprintf (1, 'Измените параметр Merge_Tolerance_Multiplier.\n');
    fclose(Fout);
    ErrCode = -1;
    return;
end

if (numel(Nset_MasterNode3) ~= 1)
    fprintf (1, '\nОшибка: недопустимое число узлов в сэте Nset_MasterNode3 !\n');
    fprintf (1, 'Измените параметр Merge_Tolerance_Multiplier.\n');
    fclose(Fout);
    ErrCode = -1;
    return;
end

if (numel(Nset_MasterNode4) ~= 1)
    fprintf (1, '\nОшибка: недопустимое число узлов в сэте Nset_MasterNode4 !\n');
    fprintf (1, 'Измените параметр Merge_Tolerance_Multiplier.\n');
    fclose(Fout);
    ErrCode = -1;
    return;
end

if (numel(Nset_MasterNode5) ~= 1)
    fprintf (1, '\nОшибка: недопустимое число узлов в сэте Nset_MasterNode5 !\n');
    fprintf (1, 'Измените параметр Merge_Tolerance_Multiplier.\n');
    fclose(Fout);
    ErrCode = -1;
    return;
end

if (numel(Nset_MasterNode6) ~= 1)
    fprintf (1, '\nОшибка: недопустимое число узлов в сэте Nset_MasterNode6 !\n');
    fprintf (1, 'Измените параметр Merge_Tolerance_Multiplier.\n');
    fclose(Fout);
    ErrCode = -1;
    return;
end

if (numel(Nset_MasterNode7) ~= 1)
    fprintf (1, '\nОшибка: недопустимое число узлов в сэте Nset_MasterNode7 !\n');
    fprintf (1, 'Измените параметр Merge_Tolerance_Multiplier.\n');
    fclose(Fout);
    ErrCode = -1;
    return;
end

if (numel(Nset_MasterNode8) ~= 1)
    fprintf (1, '\nОшибка: недопустимое число узлов в сэте Nset_MasterNode8 !\n');
    fprintf (1, 'Измените параметр Merge_Tolerance_Multiplier.\n');
    fclose(Fout);
    ErrCode = -1;
    return;
end

fprintf (Fout, '*NSet, NSet=MasterNode1, instance=%s-1, Unsorted\n', Part_Name);
Write_Set(Fout, Nset_MasterNode1);

fprintf (Fout, '*NSet, NSet=MasterNode2, instance=%s-1, Unsorted\n', Part_Name);
Write_Set(Fout, Nset_MasterNode2);

fprintf (Fout, '*NSet, NSet=MasterNode3, instance=%s-1, Unsorted\n', Part_Name);
Write_Set(Fout, Nset_MasterNode3);

fprintf (Fout, '*NSet, NSet=MasterNode4, instance=%s-1, Unsorted\n', Part_Name);
Write_Set(Fout, Nset_MasterNode4);

fprintf (Fout, '*NSet, NSet=MasterNode5, instance=%s-1, Unsorted\n', Part_Name);
Write_Set(Fout, Nset_MasterNode5);

fprintf (Fout, '*NSet, NSet=MasterNode6, instance=%s-1, Unsorted\n', Part_Name);
Write_Set(Fout, Nset_MasterNode6);

fprintf (Fout, '*NSet, NSet=MasterNode7, instance=%s-1, Unsorted\n', Part_Name);
Write_Set(Fout, Nset_MasterNode7);

fprintf (Fout, '*NSet, NSet=MasterNode8, instance=%s-1, Unsorted\n', Part_Name);
Write_Set(Fout, Nset_MasterNode8);

fprintf (Fout, '**\n');
fprintf (Fout, '** EQUATIONS\n');
fprintf (Fout, '**\n');

fprintf (Fout, '*Equation\n');
fprintf (Fout, '3\n');
fprintf (Fout, ['FaceA, 1, 1.0, FaceB, 1, -1.0, ConstraintsDriver0, 1, ' equation_format '\n'], -LX);

fprintf (Fout, '*Equation\n');
fprintf (Fout, '2\n');
fprintf (Fout, 'FaceA, 2, 1.0, FaceB, 2, -1.0\n');

fprintf (Fout, '*Equation\n');
fprintf (Fout, '2\n');
fprintf (Fout, 'FaceA, 3, 1.0, FaceB, 3, -1.0\n');

fprintf (Fout, '*Equation\n');
fprintf (Fout, '3\n');
fprintf (Fout, ['FaceC, 1, 1.0, FaceD, 1, -1.0, ConstraintsDriver3, 1, ' equation_format '\n'], -LY);

fprintf (Fout, '*Equation\n');
fprintf (Fout, '3\n');
fprintf (Fout, ['FaceC, 2, 1.0, FaceD, 2, -1.0, ConstraintsDriver1, 1, ' equation_format '\n'], -LY);

fprintf (Fout, '*Equation\n');
fprintf (Fout, '2\n');
fprintf (Fout, 'FaceC, 3, 1.0, FaceD, 3, -1.0\n');

fprintf (Fout, '*Equation\n');
fprintf (Fout, '3\n');
fprintf (Fout, ['FaceE, 1, 1.0, FaceF, 1, -1.0, ConstraintsDriver4, 1, ' equation_format '\n'], -LZ);

fprintf (Fout, '*Equation\n');
fprintf (Fout, '3\n');
fprintf (Fout, ['FaceE, 2, 1.0, FaceF, 2, -1.0, ConstraintsDriver5, 1, ' equation_format '\n'], -LZ);

fprintf (Fout, '*Equation\n');
fprintf (Fout, '3\n');
fprintf (Fout, ['FaceE, 3, 1.0, FaceF, 3, -1.0, ConstraintsDriver2, 1, ' equation_format '\n'], -LZ);

fprintf (Fout, '*Equation\n');
fprintf (Fout, '3\n');
fprintf (Fout, ['Edge2, 1, 1.0, Edge1, 1, -1.0, ConstraintsDriver0, 1, ' equation_format '\n'], -LX);

fprintf (Fout, '*Equation\n');
fprintf (Fout, '2\n');
fprintf (Fout, 'Edge2, 2, 1.0, Edge1, 2, -1.0\n');

fprintf (Fout, '*Equation\n');
fprintf (Fout, '2\n');
fprintf (Fout, 'Edge2, 3, 1.0, Edge1, 3, -1.0\n');

fprintf (Fout, '*Equation\n');
fprintf (Fout, '4\n');
fprintf (Fout, ['Edge3, 1, 1.0, Edge1, 1, -1.0, ConstraintsDriver0, 1, ' equation_format ', ConstraintsDriver3, 1, ' equation_format '\n'], -LX, -LY);

fprintf (Fout, '*Equation\n');
fprintf (Fout, '3\n');
fprintf (Fout, ['Edge3, 2, 1.0, Edge1, 2, -1.0, ConstraintsDriver1, 1, ' equation_format '\n'], -LY);

fprintf (Fout, '*Equation\n');
fprintf (Fout, '2\n');
fprintf (Fout, 'Edge3, 3, 1.0, Edge1, 3, -1.0\n');

fprintf (Fout, '*Equation\n');
fprintf (Fout, '3\n');
fprintf (Fout, ['Edge4, 1, 1.0, Edge1, 1, -1.0, ConstraintsDriver3, 1, ' equation_format '\n'], -LY);

fprintf (Fout, '*Equation\n');
fprintf (Fout, '3\n');
fprintf (Fout, ['Edge4, 2, 1.0, Edge1, 2, -1.0, ConstraintsDriver1, 1, ' equation_format '\n'], -LY);

fprintf (Fout, '*Equation\n');
fprintf (Fout, '2\n');
fprintf (Fout, 'Edge4, 3, 1.0, Edge1, 3, -1.0\n');

fprintf (Fout, '*Equation\n');
fprintf (Fout, '3\n');
fprintf (Fout, ['Edge6, 1, 1.0, Edge5, 1, -1.0, ConstraintsDriver0, 1, ' equation_format '\n'], -LX);

fprintf (Fout, '*Equation\n');
fprintf (Fout, '2\n');
fprintf (Fout, 'Edge6, 2, 1.0, Edge5, 2, -1.0\n');

fprintf (Fout, '*Equation\n');
fprintf (Fout, '2\n');
fprintf (Fout, 'Edge6, 3, 1.0, Edge5, 3, -1.0\n');

fprintf (Fout, '*Equation\n');
fprintf (Fout, '4\n');
fprintf (Fout, ['Edge7, 1, 1.0, Edge5, 1, -1.0, ConstraintsDriver4, 1, ' equation_format ', ConstraintsDriver0, 1, ' equation_format '\n'], -LZ, -LX);

fprintf (Fout, '*Equation\n');
fprintf (Fout, '3\n');
fprintf (Fout, ['Edge7, 2, 1.0, Edge5, 2, -1.0, ConstraintsDriver5, 1, ' equation_format '\n'], -LZ);

fprintf (Fout, '*Equation\n');
fprintf (Fout, '3\n');
fprintf (Fout, ['Edge7, 3, 1.0, Edge5, 3, -1.0, ConstraintsDriver2, 1, ' equation_format '\n'], -LZ);

fprintf (Fout, '*Equation\n');
fprintf (Fout, '3\n');
fprintf (Fout, ['Edge8, 1, 1.0, Edge5, 1, -1.0, ConstraintsDriver4, 1, ' equation_format '\n'], -LZ);

fprintf (Fout, '*Equation\n');
fprintf (Fout, '3\n');
fprintf (Fout, ['Edge8, 2, 1.0, Edge5, 2, -1.0, ConstraintsDriver5, 1, ' equation_format '\n'], -LZ);

fprintf (Fout, '*Equation\n');
fprintf (Fout, '3\n');
fprintf (Fout, ['Edge8, 3, 1.0, Edge5, 3, -1.0, ConstraintsDriver2, 1, ' equation_format '\n'], -LZ);

fprintf (Fout, '*Equation\n');
fprintf (Fout, '3\n');
fprintf (Fout, ['Edge10, 1, 1.0, Edge9, 1, -1.0, ConstraintsDriver3, 1, ' equation_format '\n'], -LY);

fprintf (Fout, '*Equation\n');
fprintf (Fout, '3\n');
fprintf (Fout, ['Edge10, 2, 1.0, Edge9, 2, -1.0, ConstraintsDriver1, 1, ' equation_format '\n'], -LY);

fprintf (Fout, '*Equation\n');
fprintf (Fout, '2\n');
fprintf (Fout, 'Edge10, 3, 1.0, Edge9, 3, -1.0\n');

fprintf (Fout, '*Equation\n');
fprintf (Fout, '4\n');
fprintf (Fout, ['Edge11, 1, 1.0, Edge9, 1, -1.0, ConstraintsDriver3, 1, ' equation_format ', ConstraintsDriver4, 1, ' equation_format '\n'], -LY, -LZ);

fprintf (Fout, '*Equation\n');
fprintf (Fout, '4\n');
fprintf (Fout, ['Edge11, 2, 1.0, Edge9, 2, -1.0, ConstraintsDriver1, 1, ' equation_format ', ConstraintsDriver5, 1, ' equation_format '\n'], -LY, -LZ);

fprintf (Fout, '*Equation\n');
fprintf (Fout, '3\n');
fprintf (Fout, ['Edge11, 3, 1.0, Edge9, 3, -1.0, ConstraintsDriver2, 1, ' equation_format '\n'], -LZ);

fprintf (Fout, '*Equation\n');
fprintf (Fout, '3\n');
fprintf (Fout, ['Edge12, 1, 1.0, Edge9, 1, -1.0, ConstraintsDriver4, 1, ' equation_format '\n'], -LZ);

fprintf (Fout, '*Equation\n');
fprintf (Fout, '3\n');
fprintf (Fout, ['Edge12, 2, 1.0, Edge9, 2, -1.0, ConstraintsDriver5, 1, ' equation_format '\n'], -LZ);

fprintf (Fout, '*Equation\n');
fprintf (Fout, '3\n');
fprintf (Fout, ['Edge12, 3, 1.0, Edge9, 3, -1.0, ConstraintsDriver2, 1, ' equation_format '\n'], -LZ);

fprintf (Fout, '*Equation\n');
fprintf (Fout, '3\n');
fprintf (Fout, ['MasterNode2, 1, 1.0, MasterNode1, 1, -1.0, ConstraintsDriver0, 1, ' equation_format '\n'], -LX);

fprintf (Fout, '*Equation\n');
fprintf (Fout, '2\n');
fprintf (Fout, 'MasterNode2, 2, 1.0, MasterNode1, 2, -1.0\n');

fprintf (Fout, '*Equation\n');
fprintf (Fout, '2\n');
fprintf (Fout, 'MasterNode2, 3, 1.0, MasterNode1, 3, -1.0\n');

fprintf (Fout, '*Equation\n');
fprintf (Fout, '4\n');
fprintf (Fout, ['MasterNode3, 1, 1.0, MasterNode1, 1, -1.0, ConstraintsDriver0, 1, ' equation_format ', ConstraintsDriver3, 1, ' equation_format '\n'], -LX, -LY);

fprintf (Fout, '*Equation\n');
fprintf (Fout, '3\n');
fprintf (Fout, ['MasterNode3, 2, 1.0, MasterNode1, 2, -1.0, ConstraintsDriver1, 1, ' equation_format '\n'], -LY);

fprintf (Fout, '*Equation\n');
fprintf (Fout, '2\n');
fprintf (Fout, 'MasterNode3, 3, 1.0, MasterNode1, 3, -1.0\n');

fprintf (Fout, '*Equation\n');
fprintf (Fout, '3\n');
fprintf (Fout, ['MasterNode4, 1, 1.0, MasterNode1, 1, -1.0, ConstraintsDriver3, 1, ' equation_format '\n'], -LY);

fprintf (Fout, '*Equation\n');
fprintf (Fout, '3\n');
fprintf (Fout, ['MasterNode4, 2, 1.0, MasterNode1, 2, -1.0, ConstraintsDriver1, 1, ' equation_format '\n'], -LY);

fprintf (Fout, '*Equation\n');
fprintf (Fout, '2\n');
fprintf (Fout, 'MasterNode4, 3, 1.0, MasterNode1, 3, -1.0\n');

fprintf (Fout, '*Equation\n');
fprintf (Fout, '3\n');
fprintf (Fout, ['MasterNode5, 1, 1.0, MasterNode1, 1, -1.0, ConstraintsDriver4, 1, ' equation_format '\n'], -LZ);

fprintf (Fout, '*Equation\n');
fprintf (Fout, '3\n');
fprintf (Fout, ['MasterNode5, 2, 1.0, MasterNode1, 2, -1.0, ConstraintsDriver5, 1, ' equation_format '\n'], -LZ);

fprintf (Fout, '*Equation\n');
fprintf (Fout, '3\n');
fprintf (Fout, ['MasterNode5, 3, 1.0, MasterNode1, 3, -1.0, ConstraintsDriver2, 1, ' equation_format '\n'], -LZ);

fprintf (Fout, '*Equation\n');
fprintf (Fout, '4\n');
fprintf (Fout, ['MasterNode6, 1, 1.0, MasterNode1, 1, -1.0, ConstraintsDriver4, 1, ' equation_format ', ConstraintsDriver0, 1, ' equation_format '\n'], -LZ, -LX);

fprintf (Fout, '*Equation\n');
fprintf (Fout, '3\n');
fprintf (Fout, ['MasterNode6, 2, 1.0, MasterNode1, 2, -1.0, ConstraintsDriver5, 1, ' equation_format '\n'], -LZ);

fprintf (Fout, '*Equation\n');
fprintf (Fout, '3\n');
fprintf (Fout, ['MasterNode6, 3, 1.0, MasterNode1, 3, -1.0, ConstraintsDriver2, 1, ' equation_format '\n'], -LZ);

fprintf (Fout, '*Equation\n');
fprintf (Fout, '5\n');
fprintf (Fout, ['MasterNode7, 1, 1.0, MasterNode1, 1, -1.0, ConstraintsDriver0, 1, ' equation_format ', ConstraintsDriver4, 1, ' equation_format ',\n'], -LX, -LZ);
fprintf (Fout, ['ConstraintsDriver3, 1, ' equation_format '\n'], -LY);

fprintf (Fout, '*Equation\n');
fprintf (Fout, '4\n');
fprintf (Fout, ['MasterNode7, 2, 1.0, MasterNode1, 2, -1.0, ConstraintsDriver1, 1, ' equation_format ', ConstraintsDriver5, 1, ' equation_format '\n'], -LY, -LZ);

fprintf (Fout, '*Equation\n');
fprintf (Fout, '3\n');
fprintf (Fout, ['MasterNode7, 3, 1.0, MasterNode1, 3, -1.0, ConstraintsDriver2, 1, ' equation_format '\n'], -LZ);

fprintf (Fout, '*Equation\n');
fprintf (Fout, '4\n');
fprintf (Fout, ['MasterNode8, 1, 1.0, MasterNode1, 1, -1.0, ConstraintsDriver3, 1, ' equation_format ', ConstraintsDriver4, 1, ' equation_format '\n'], -LY, -LZ);

fprintf (Fout, '*Equation\n');
fprintf (Fout, '4\n');
fprintf (Fout, ['MasterNode8, 2, 1.0, MasterNode1, 2, -1.0, ConstraintsDriver1, 1, ' equation_format ', ConstraintsDriver5, 1, ' equation_format '\n'], -LY, -LZ);

fprintf (Fout, '*Equation\n');
fprintf (Fout, '3\n');
fprintf (Fout, ['MasterNode8, 3, 1.0, MasterNode1, 3, -1.0, ConstraintsDriver2, 1, ' equation_format '\n'], -LZ);

if (strcmp(Mechanical_BC_TYPE, 'HARD'))
    
    % Если присутствует ограничение по X
    if (~isempty(strfind(Constraint_Directions, 'X'))) %#ok<STREMP>

        for i=1:size(Nset_FaceB, 1)

            fprintf (Fout, '*Equation\n');
            fprintf (Fout, '3\n');
            fprintf (Fout, ['%s-1.%i, 1, 1.0, ConstraintsDriver3, 1, ' equation_format ', ConstraintsDriver4, 1, ' equation_format '\n'], ...
                           Part_Name, Nset_FaceB(i), ...
                           NODES(Nset_MasterNode1(1),3) - NODES(Nset_FaceB(i),3), ...
                           NODES(Nset_MasterNode1(1),4) - NODES(Nset_FaceB(i),4));

        end
        
        for i=1:size(Nset_Edge1, 1)

            fprintf (Fout, '*Equation\n');
            fprintf (Fout, '2\n');
            fprintf (Fout, ['%s-1.%i, 1, 1.0, ConstraintsDriver4, 1, ' equation_format '\n'], ...
                           Part_Name, Nset_Edge1(i), ...
                           NODES(Nset_MasterNode1(1),4) - NODES(Nset_Edge1(i),4));

        end
        
        for i=1:size(Nset_Edge5, 1)

            fprintf (Fout, '*Equation\n');
            fprintf (Fout, '2\n');
            fprintf (Fout, ['%s-1.%i, 1, 1.0, ConstraintsDriver3, 1, ' equation_format '\n'], ...
                           Part_Name, Nset_Edge5(i), ...
                           NODES(Nset_MasterNode1(1),3) - NODES(Nset_Edge5(i),3));

        end

    end

    % Если присутствует ограничение по Y
    if (~isempty(strfind(Constraint_Directions, 'Y'))) %#ok<STREMP>

        for i=1:size(Nset_FaceD, 1)

            fprintf (Fout, '*Equation\n');
            fprintf (Fout, '2\n');
            fprintf (Fout, ['%s-1.%i, 2, 1.0, ConstraintsDriver5, 1, ' equation_format '\n'], ...
                           Part_Name, Nset_FaceD(i), ...
                           NODES(Nset_MasterNode1(1),4) - NODES(Nset_FaceD(i),4));

        end
        
        for i=1:size(Nset_Edge1, 1)

            fprintf (Fout, '*Equation\n');
            fprintf (Fout, '2\n');
            fprintf (Fout, ['%s-1.%i, 2, 1.0, ConstraintsDriver5, 1, ' equation_format '\n'], ...
                           Part_Name, Nset_Edge1(i), ...
                           NODES(Nset_MasterNode1(1),4) - NODES(Nset_Edge1(i),4));

        end

    end

end

if (strcmp(Cell_Mesh_Type, 'SHELL'))

    fprintf (Fout, '*Equation\n');
    fprintf (Fout, '2\n');
    fprintf (Fout, 'FaceA, 4, 1.0, FaceB, 4, -1.0\n');
    fprintf (Fout, '*Equation\n');
    fprintf (Fout, '2\n');
    fprintf (Fout, 'FaceA, 5, 1.0, FaceB, 5, -1.0\n');
    fprintf (Fout, '*Equation\n');
    fprintf (Fout, '2\n');
    fprintf (Fout, 'FaceA, 6, 1.0, FaceB, 6, -1.0\n');

    fprintf (Fout, '*Equation\n');
    fprintf (Fout, '2\n');
    fprintf (Fout, 'FaceC, 4, 1.0, FaceD, 4, -1.0\n');
    fprintf (Fout, '*Equation\n');
    fprintf (Fout, '2\n');
    fprintf (Fout, 'FaceC, 5, 1.0, FaceD, 5, -1.0\n');
    fprintf (Fout, '*Equation\n');
    fprintf (Fout, '2\n');
    fprintf (Fout, 'FaceC, 6, 1.0, FaceD, 6, -1.0\n');

    fprintf (Fout, '*Equation\n');
    fprintf (Fout, '2\n');
    fprintf (Fout, 'FaceE, 4, 1.0, FaceF, 4, -1.0\n');
    fprintf (Fout, '*Equation\n');
    fprintf (Fout, '2\n');
    fprintf (Fout, 'FaceE, 5, 1.0, FaceF, 5, -1.0\n');
    fprintf (Fout, '*Equation\n');
    fprintf (Fout, '2\n');
    fprintf (Fout, 'FaceE, 6, 1.0, FaceF, 6, -1.0\n');

    % ---

    fprintf (Fout, '*Equation\n');
    fprintf (Fout, '2\n');
    fprintf (Fout, 'Edge4, 4, 1.0, Edge3, 4, -1.0\n');
    fprintf (Fout, '*Equation\n');
    fprintf (Fout, '2\n');
    fprintf (Fout, 'Edge4, 5, 1.0, Edge3, 5, -1.0\n');
    fprintf (Fout, '*Equation\n');
    fprintf (Fout, '2\n');
    fprintf (Fout, 'Edge4, 6, 1.0, Edge3, 6, -1.0\n');

    fprintf (Fout, '*Equation\n');
    fprintf (Fout, '2\n');
    fprintf (Fout, 'Edge3, 4, 1.0, Edge2, 4, -1.0\n');
    fprintf (Fout, '*Equation\n');
    fprintf (Fout, '2\n');
    fprintf (Fout, 'Edge3, 5, 1.0, Edge2, 5, -1.0\n');
    fprintf (Fout, '*Equation\n');
    fprintf (Fout, '2\n');
    fprintf (Fout, 'Edge3, 6, 1.0, Edge2, 6, -1.0\n');

    fprintf (Fout, '*Equation\n');
    fprintf (Fout, '2\n');
    fprintf (Fout, 'Edge2, 4, 1.0, Edge1, 4, -1.0\n');
    fprintf (Fout, '*Equation\n');
    fprintf (Fout, '2\n');
    fprintf (Fout, 'Edge2, 5, 1.0, Edge1, 5, -1.0\n');
    fprintf (Fout, '*Equation\n');
    fprintf (Fout, '2\n');
    fprintf (Fout, 'Edge2, 6, 1.0, Edge1, 6, -1.0\n');

    % ---

    fprintf (Fout, '*Equation\n');
    fprintf (Fout, '2\n');
    fprintf (Fout, 'Edge8, 4, 1.0, Edge7, 4, -1.0\n');
    fprintf (Fout, '*Equation\n');
    fprintf (Fout, '2\n');
    fprintf (Fout, 'Edge8, 5, 1.0, Edge7, 5, -1.0\n');
    fprintf (Fout, '*Equation\n');
    fprintf (Fout, '2\n');
    fprintf (Fout, 'Edge8, 6, 1.0, Edge7, 6, -1.0\n');

    fprintf (Fout, '*Equation\n');
    fprintf (Fout, '2\n');
    fprintf (Fout, 'Edge7, 4, 1.0, Edge6, 4, -1.0\n');
    fprintf (Fout, '*Equation\n');
    fprintf (Fout, '2\n');
    fprintf (Fout, 'Edge7, 5, 1.0, Edge6, 5, -1.0\n');
    fprintf (Fout, '*Equation\n');
    fprintf (Fout, '2\n');
    fprintf (Fout, 'Edge7, 6, 1.0, Edge6, 6, -1.0\n');

    fprintf (Fout, '*Equation\n');
    fprintf (Fout, '2\n');
    fprintf (Fout, 'Edge6, 4, 1.0, Edge5, 4, -1.0\n');
    fprintf (Fout, '*Equation\n');
    fprintf (Fout, '2\n');
    fprintf (Fout, 'Edge6, 5, 1.0, Edge5, 5, -1.0\n');
    fprintf (Fout, '*Equation\n');
    fprintf (Fout, '2\n');
    fprintf (Fout, 'Edge6, 6, 1.0, Edge5, 6, -1.0\n');

    % ---

    fprintf (Fout, '*Equation\n');
    fprintf (Fout, '2\n');
    fprintf (Fout, 'Edge12, 4, 1.0, Edge11, 4, -1.0\n');
    fprintf (Fout, '*Equation\n');
    fprintf (Fout, '2\n');
    fprintf (Fout, 'Edge12, 5, 1.0, Edge11, 5, -1.0\n');
    fprintf (Fout, '*Equation\n');
    fprintf (Fout, '2\n');
    fprintf (Fout, 'Edge12, 6, 1.0, Edge11, 6, -1.0\n');

    fprintf (Fout, '*Equation\n');
    fprintf (Fout, '2\n');
    fprintf (Fout, 'Edge11, 4, 1.0, Edge10, 4, -1.0\n');
    fprintf (Fout, '*Equation\n');
    fprintf (Fout, '2\n');
    fprintf (Fout, 'Edge11, 5, 1.0, Edge10, 5, -1.0\n');
    fprintf (Fout, '*Equation\n');
    fprintf (Fout, '2\n');
    fprintf (Fout, 'Edge11, 6, 1.0, Edge10, 6, -1.0\n');

    fprintf (Fout, '*Equation\n');
    fprintf (Fout, '2\n');
    fprintf (Fout, 'Edge10, 4, 1.0, Edge9, 4, -1.0\n');
    fprintf (Fout, '*Equation\n');
    fprintf (Fout, '2\n');
    fprintf (Fout, 'Edge10, 5, 1.0, Edge9, 5, -1.0\n');
    fprintf (Fout, '*Equation\n');
    fprintf (Fout, '2\n');
    fprintf (Fout, 'Edge10, 6, 1.0, Edge9, 6, -1.0\n');

    % ---

    fprintf (Fout, '*Equation\n');
    fprintf (Fout, '2\n');
    fprintf (Fout, 'MasterNode8, 4, 1.0, MasterNode7, 4, -1.0\n');
    fprintf (Fout, '*Equation\n');
    fprintf (Fout, '2\n');
    fprintf (Fout, 'MasterNode8, 5, 1.0, MasterNode7, 5, -1.0\n');
    fprintf (Fout, '*Equation\n');
    fprintf (Fout, '2\n');
    fprintf (Fout, 'MasterNode8, 6, 1.0, MasterNode7, 6, -1.0\n');

    fprintf (Fout, '*Equation\n');
    fprintf (Fout, '2\n');
    fprintf (Fout, 'MasterNode7, 4, 1.0, MasterNode6, 4, -1.0\n');
    fprintf (Fout, '*Equation\n');
    fprintf (Fout, '2\n');
    fprintf (Fout, 'MasterNode7, 5, 1.0, MasterNode6, 5, -1.0\n');
    fprintf (Fout, '*Equation\n');
    fprintf (Fout, '2\n');
    fprintf (Fout, 'MasterNode7, 6, 1.0, MasterNode6, 6, -1.0\n');

    fprintf (Fout, '*Equation\n');
    fprintf (Fout, '2\n');
    fprintf (Fout, 'MasterNode6, 4, 1.0, MasterNode5, 4, -1.0\n');
    fprintf (Fout, '*Equation\n');
    fprintf (Fout, '2\n');
    fprintf (Fout, 'MasterNode6, 5, 1.0, MasterNode5, 5, -1.0\n');
    fprintf (Fout, '*Equation\n');
    fprintf (Fout, '2\n');
    fprintf (Fout, 'MasterNode6, 6, 1.0, MasterNode5, 6, -1.0\n');

    fprintf (Fout, '*Equation\n');
    fprintf (Fout, '2\n');
    fprintf (Fout, 'MasterNode5, 4, 1.0, MasterNode4, 4, -1.0\n');
    fprintf (Fout, '*Equation\n');
    fprintf (Fout, '2\n');
    fprintf (Fout, 'MasterNode5, 5, 1.0, MasterNode4, 5, -1.0\n');
    fprintf (Fout, '*Equation\n');
    fprintf (Fout, '2\n');
    fprintf (Fout, 'MasterNode5, 6, 1.0, MasterNode4, 6, -1.0\n');

    fprintf (Fout, '*Equation\n');
    fprintf (Fout, '2\n');
    fprintf (Fout, 'MasterNode4, 4, 1.0, MasterNode3, 4, -1.0\n');
    fprintf (Fout, '*Equation\n');
    fprintf (Fout, '2\n');
    fprintf (Fout, 'MasterNode4, 5, 1.0, MasterNode3, 5, -1.0\n');
    fprintf (Fout, '*Equation\n');
    fprintf (Fout, '2\n');
    fprintf (Fout, 'MasterNode4, 6, 1.0, MasterNode3, 6, -1.0\n');

    fprintf (Fout, '*Equation\n');
    fprintf (Fout, '2\n');
    fprintf (Fout, 'MasterNode3, 4, 1.0, MasterNode2, 4, -1.0\n');
    fprintf (Fout, '*Equation\n');
    fprintf (Fout, '2\n');
    fprintf (Fout, 'MasterNode3, 5, 1.0, MasterNode2, 5, -1.0\n');
    fprintf (Fout, '*Equation\n');
    fprintf (Fout, '2\n');
    fprintf (Fout, 'MasterNode3, 6, 1.0, MasterNode2, 6, -1.0\n');

    fprintf (Fout, '*Equation\n');
    fprintf (Fout, '2\n');
    fprintf (Fout, 'MasterNode2, 4, 1.0, MasterNode1, 4, -1.0\n');
    fprintf (Fout, '*Equation\n');
    fprintf (Fout, '2\n');
    fprintf (Fout, 'MasterNode2, 5, 1.0, MasterNode1, 5, -1.0\n');
    fprintf (Fout, '*Equation\n');
    fprintf (Fout, '2\n');
    fprintf (Fout, 'MasterNode2, 6, 1.0, MasterNode1, 6, -1.0\n');
    
    if (strcmp(Mechanical_BC_TYPE, 'HARD'))
        
        % Если присутствует ограничение по X
        if (~isempty(strfind(Constraint_Directions, 'X'))) %#ok<STREMP>

            fprintf (Fout, '*Equation\n');
            fprintf (Fout, '2\n');
            fprintf (Fout, 'FaceB, 5, 1.0, ConstraintsDriver4, 1, -1.0\n');
            
            fprintf (Fout, '*Equation\n');
            fprintf (Fout, '2\n');
            fprintf (Fout, 'FaceB, 6, 1.0, ConstraintsDriver3, 1, 1.0\n');
            
            fprintf (Fout, '*Equation\n');
            fprintf (Fout, '2\n');
            fprintf (Fout, 'Edge1, 5, 1.0, ConstraintsDriver4, 1, -1.0\n');
            
            fprintf (Fout, '*Equation\n');
            fprintf (Fout, '2\n');
            fprintf (Fout, 'Edge5, 6, 1.0, ConstraintsDriver3, 1, 1.0\n');

        end

        % Если присутствует ограничение по Y
        if (~isempty(strfind(Constraint_Directions, 'Y'))) %#ok<STREMP>

            fprintf (Fout, '*Equation\n');
            fprintf (Fout, '2\n');
            fprintf (Fout, 'FaceD, 4, 1.0, ConstraintsDriver5, 1, 1.0\n');
            
            fprintf (Fout, '*Equation\n');
            fprintf (Fout, '2\n');
            fprintf (Fout, 'Edge1, 4, 1.0, ConstraintsDriver5, 1, 1.0\n');

        end
        
        % Если есть ограничение по X, но нет по Y
        if ((~isempty(strfind(Constraint_Directions, 'X'))) && isempty(strfind(Constraint_Directions, 'Y'))) %#ok<STREMP>

            fprintf (Fout, '*Equation\n');
            fprintf (Fout, '2\n');
            fprintf (Fout, 'Edge1, 6, 1.0, ConstraintsDriver3, 1, 1.0\n');
            
            fprintf (Fout, '*Equation\n');
            fprintf (Fout, '2\n');
            fprintf (Fout, 'MasterNode1, 6, 1.0, ConstraintsDriver3, 1, 1.0\n');

        end

        % Если есть ограничение по X, но нет по Z
        if ((~isempty(strfind(Constraint_Directions, 'X'))) && isempty(strfind(Constraint_Directions, 'Z'))) %#ok<STREMP>

            fprintf (Fout, '*Equation\n');
            fprintf (Fout, '2\n');
            fprintf (Fout, 'Edge5, 5, 1.0, ConstraintsDriver4, 1, -1.0\n');
            
            fprintf (Fout, '*Equation\n');
            fprintf (Fout, '2\n');
            fprintf (Fout, 'MasterNode1, 5, 1.0, ConstraintsDriver4, 1, -1.0\n');

        end

        % Если есть ограничение по Y, но нет по Z
        if ((~isempty(strfind(Constraint_Directions, 'Y'))) && isempty(strfind(Constraint_Directions, 'Z'))) %#ok<STREMP>

            fprintf (Fout, '*Equation\n');
            fprintf (Fout, '2\n');
            fprintf (Fout, 'Edge9, 4, 1.0, ConstraintsDriver5, 1, 1.0\n');
            
            fprintf (Fout, '*Equation\n');
            fprintf (Fout, '2\n');
            fprintf (Fout, 'MasterNode1, 4, 1.0, ConstraintsDriver5, 1, 1.0\n');

        end

    end

end

fprintf (Fout, '*End Assembly\n');

fprintf (Fout, '**\n');
fprintf (Fout, '** MATERIALS\n');
fprintf (Fout, '**\n');

Mat_Numbers = unique(Stacking(:,3));

format_line = [mat_prop_format ', ' mat_prop_format ', ' mat_prop_format ', ' mat_prop_format ', ' mat_prop_format ', ' mat_prop_format ', ' mat_prop_format ', ' mat_prop_format '\n'];

for i=1:numel(Mat_Numbers)
    
    fprintf (Fout, '*Material, name=Material_%i\n', Mat_Numbers(i));
    fprintf (Fout, '*Elastic, type=ENGINEERING CONSTANTS\n');
    fprintf (Fout, format_line, Materials{Mat_Numbers(i)}.E1, Materials{Mat_Numbers(i)}.E2, Materials{Mat_Numbers(i)}.E3, ...
                                Materials{Mat_Numbers(i)}.nu12, Materials{Mat_Numbers(i)}.nu13, Materials{Mat_Numbers(i)}.nu23, ...
                                Materials{Mat_Numbers(i)}.G12, Materials{Mat_Numbers(i)}.G13);
    fprintf (Fout, [mat_prop_format ',\n'], Materials{Mat_Numbers(i)}.G23);
    fprintf (Fout, '*Expansion, type=ORTHO\n');
    
    switch CTE_Mode
        case 'ALPHA'
            fprintf (Fout, [mat_prop_format ', ' mat_prop_format ', ' mat_prop_format '\n'], Materials{Mat_Numbers(i)}.alpha11, Materials{Mat_Numbers(i)}.alpha22, Materials{Mat_Numbers(i)}.alpha33);
        case 'BETA'
            fprintf (Fout, [mat_prop_format ', ' mat_prop_format ', ' mat_prop_format '\n'], Materials{Mat_Numbers(i)}.beta11, Materials{Mat_Numbers(i)}.beta22, Materials{Mat_Numbers(i)}.beta33);
        otherwise
            fprintf (1, '\nОшибка: недопустимое значение параметра CTE_Mode !\n');
            fclose(Fout);
            ErrCode = -1;
            return;
    end

end

fprintf (Fout, '**\n');
fprintf (Fout, '** BOUNDARY CONDITIONS\n');
fprintf (Fout, '**\n');

fprintf (Fout, '** Name: Translation stop Vertex 1 Type: Displacement/Rotation\n');
fprintf (Fout, '*Boundary\n');
fprintf (Fout, 'MasterNode1, 1, 1\n');
fprintf (Fout, 'MasterNode1, 2, 2\n');
fprintf (Fout, 'MasterNode1, 3, 3\n');

if (strcmp(Mechanical_BC_TYPE, 'HARD'))
    
    % Если присутствует ограничение по Y
    if (~isempty(strfind(Constraint_Directions, 'Y'))) %#ok<STREMP>

        fprintf (Fout, 'Edge9, 2, 2\n');

    end

    % Если присутствует ограничение по Z
    if (~isempty(strfind(Constraint_Directions, 'Z'))) %#ok<STREMP>

        fprintf (Fout, 'FaceF, 3, 3\n');
        fprintf (Fout, 'Edge5, 3, 3\n');
        fprintf (Fout, 'Edge9, 3, 3\n');

    end
    
    if (strcmp(Cell_Mesh_Type, 'SHELL'))
        
        % Если присутствует ограничение по Y
        if (~isempty(strfind(Constraint_Directions, 'Y'))) %#ok<STREMP>

            fprintf (Fout, 'FaceD, 6, 6\n');
            fprintf (Fout, 'Edge9, 6, 6\n');

        end

        % Если присутствует ограничение по Z
        if (~isempty(strfind(Constraint_Directions, 'Z'))) %#ok<STREMP>

            fprintf (Fout, 'FaceF, 4, 4\n');
            fprintf (Fout, 'FaceF, 5, 5\n');
            fprintf (Fout, 'Edge5, 4, 4\n');
            fprintf (Fout, 'Edge9, 5, 5\n');

        end
        
        % Если есть ограничение по Y, но нет по X
        if ((~isempty(strfind(Constraint_Directions, 'Y'))) && isempty(strfind(Constraint_Directions, 'X'))) %#ok<STREMP>

            fprintf (Fout, 'Edge1, 6, 6\n');
            fprintf (Fout, 'MasterNode1, 6, 6\n');

        end
        
        % Если есть ограничение по Z, но нет по X
        if ((~isempty(strfind(Constraint_Directions, 'Z'))) && isempty(strfind(Constraint_Directions, 'X'))) %#ok<STREMP>

            fprintf (Fout, 'Edge5, 5, 5\n');
            fprintf (Fout, 'MasterNode1, 5, 5\n');

        end
        
        % Если есть ограничение по Z, но нет по Y
        if ((~isempty(strfind(Constraint_Directions, 'Z'))) && isempty(strfind(Constraint_Directions, 'Y'))) %#ok<STREMP>

            fprintf (Fout, 'Edge9, 4, 4\n');
            fprintf (Fout, 'MasterNode1, 4, 4\n');

        end
        
    end
    
end

fprintf (Fout, '**\n');
fprintf (Fout, '** PREDEFINED FIELDS\n');
fprintf (Fout, '**\n');

fprintf (Fout, '** Name: Initial temperature for all nodes   Type: Temperature\n');
fprintf (Fout, '*Initial Conditions, type=TEMPERATURE\n');
fprintf (Fout, '%s-1.ALL_NODES, 0.\n', Part_Name);

fprintf (Fout, '** ----------------------------------------------------------------\n');

fprintf (Fout, '**\n');
fprintf (Fout, '** STEP: Isothermal linear perturbation step\n');
fprintf (Fout, '**\n');

fprintf (Fout, '*Step, name="Isothermal linear perturbation step", nlgeom=NO, perturbation\n');
fprintf (Fout, '*Static\n');

fprintf (Fout, '**\n');
fprintf (Fout, '** OUTPUT REQUESTS\n');
fprintf (Fout, '**\n');

fprintf (Fout, '*Output, field\n');
fprintf (Fout, '*Node Output\n');
fprintf (Fout, 'CF, RF, U\n');
fprintf (Fout, '*Element Output, directions=YES\n');
fprintf (Fout, 'E, S\n');

fprintf (Fout, '*Output, history, frequency=0\n');

fprintf (Fout, '*NODE PRINT, GLOBAL=YES, FREQUENCY=99999, NSET=ConstraintsDriver0\n');
fprintf (Fout, 'U,\n');
fprintf (Fout, '*NODE PRINT, GLOBAL=YES, FREQUENCY=99999, NSET=ConstraintsDriver1\n');
fprintf (Fout, 'U,\n');
fprintf (Fout, '*NODE PRINT, GLOBAL=YES, FREQUENCY=99999, NSET=ConstraintsDriver2\n');
fprintf (Fout, 'U,\n');
fprintf (Fout, '*NODE PRINT, GLOBAL=YES, FREQUENCY=99999, NSET=ConstraintsDriver3\n');
fprintf (Fout, 'U,\n');
fprintf (Fout, '*NODE PRINT, GLOBAL=YES, FREQUENCY=99999, NSET=ConstraintsDriver4\n');
fprintf (Fout, 'U,\n');
fprintf (Fout, '*NODE PRINT, GLOBAL=YES, FREQUENCY=99999, NSET=ConstraintsDriver5\n');
fprintf (Fout, 'U,\n');

fprintf (Fout, '**\n');
fprintf (Fout, '** LOAD CASES\n');
fprintf (Fout, '**\n');

for i=0:5
    
    fprintf (Fout, '*Load Case, name=Load%i\n', i);
    fprintf (Fout, '** Name: Translation stop Vertex 1 Type: Displacement/Rotation   Scale factor: 1\n');
    fprintf (Fout, '*Boundary, op=NEW\n');
    fprintf (Fout, 'MasterNode1, 1, 1\n');
    fprintf (Fout, 'MasterNode1, 2, 2\n');
    fprintf (Fout, 'MasterNode1, 3, 3\n');
    
    if (strcmp(Mechanical_BC_TYPE, 'HARD'))

        % Если присутствует ограничение по Y
        if (~isempty(strfind(Constraint_Directions, 'Y'))) %#ok<STREMP>

            fprintf (Fout, 'Edge9, 2, 2\n');

        end

        % Если присутствует ограничение по Z
        if (~isempty(strfind(Constraint_Directions, 'Z'))) %#ok<STREMP>

            fprintf (Fout, 'FaceF, 3, 3\n');
            fprintf (Fout, 'Edge5, 3, 3\n');
            fprintf (Fout, 'Edge9, 3, 3\n');

        end

        if (strcmp(Cell_Mesh_Type, 'SHELL'))

            % Если присутствует ограничение по Y
            if (~isempty(strfind(Constraint_Directions, 'Y'))) %#ok<STREMP>

                fprintf (Fout, 'FaceD, 6, 6\n');
                fprintf (Fout, 'Edge9, 6, 6\n');

            end

            % Если присутствует ограничение по Z
            if (~isempty(strfind(Constraint_Directions, 'Z'))) %#ok<STREMP>

                fprintf (Fout, 'FaceF, 4, 4\n');
                fprintf (Fout, 'FaceF, 5, 5\n');
                fprintf (Fout, 'Edge5, 4, 4\n');
                fprintf (Fout, 'Edge9, 5, 5\n');

            end

            % Если есть ограничение по Y, но нет по X
            if ((~isempty(strfind(Constraint_Directions, 'Y'))) && isempty(strfind(Constraint_Directions, 'X'))) %#ok<STREMP>

                fprintf (Fout, 'Edge1, 6, 6\n');
                fprintf (Fout, 'MasterNode1, 6, 6\n');

            end

            % Если есть ограничение по Z, но нет по X
            if ((~isempty(strfind(Constraint_Directions, 'Z'))) && isempty(strfind(Constraint_Directions, 'X'))) %#ok<STREMP>

                fprintf (Fout, 'Edge5, 5, 5\n');
                fprintf (Fout, 'MasterNode1, 5, 5\n');

            end

            % Если есть ограничение по Z, но нет по Y
            if ((~isempty(strfind(Constraint_Directions, 'Z'))) && isempty(strfind(Constraint_Directions, 'Y'))) %#ok<STREMP>

                fprintf (Fout, 'Edge9, 4, 4\n');
                fprintf (Fout, 'MasterNode1, 4, 4\n');

            end

        end

    end
    
    fprintf (Fout, '** Name: Force_%i   Type: Concentrated force   Scale factor: 1\n', i);
    fprintf (Fout, '*Cload\n');
    fprintf (Fout, ['ConstraintsDriver%i, 1, ' bc_format '\n'], i, LX*LY*LZ);
    fprintf (Fout, '*End Load Case\n\n');
    
end

fprintf (Fout, '*End Step\n');

fprintf (Fout, '** ----------------------------------------------------------------\n');

fprintf (Fout, '**\n');
fprintf (Fout, '** STEP: Thermomechanical step\n');
fprintf (Fout, '**\n');

fprintf (Fout, '*Step, name="Thermomechanical step", nlgeom=NO, perturbation\n');
fprintf (Fout, '*Static\n');

fprintf (Fout, '**\n');
fprintf (Fout, '** PREDEFINED FIELDS\n');
fprintf (Fout, '**\n');

fprintf (Fout, '** Name: Temperature for all nodes   Type: Temperature\n');
fprintf (Fout, '*Temperature\n');
fprintf (Fout, '%s-1.ALL_NODES, 1.\n', Part_Name);

fprintf (Fout, '**\n');
fprintf (Fout, '** OUTPUT REQUESTS\n');
fprintf (Fout, '**\n');

fprintf (Fout, '*Output, field\n');
fprintf (Fout, '*Node Output\n');
fprintf (Fout, 'CF, RF, U\n');
fprintf (Fout, '*Element Output, directions=YES\n');
fprintf (Fout, 'E, S\n');

fprintf (Fout, '*Output, history, frequency=0\n');

fprintf (Fout, '*NODE PRINT, GLOBAL=YES, FREQUENCY=99999, NSET=ConstraintsDriver0\n');
fprintf (Fout, 'U,\n');
fprintf (Fout, '*NODE PRINT, GLOBAL=YES, FREQUENCY=99999, NSET=ConstraintsDriver1\n');
fprintf (Fout, 'U,\n');
fprintf (Fout, '*NODE PRINT, GLOBAL=YES, FREQUENCY=99999, NSET=ConstraintsDriver2\n');
fprintf (Fout, 'U,\n');

fprintf (Fout, '*End Step\n');

fclose(Fout);
